package algo.array;

public class GenericArray<T> {
    private T[] data;
    private int size;

    public GenericArray(int capacity) {
        data = (T[]) new Object[capacity];
        size = 0;
    }

    public GenericArray() {
        this(10);
    }

    public int getCapacity() {
        return this.data.length;
    }

    public int count() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void set(int index, T e) {
        checkIndex(index);
        data[index] = e;
    }

    public T get(int index) {
        checkIndex(index);
        return data[index];
    }

    public boolean contains(T e) {
        for (int i=0; i < size; ++i) {
            if (data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    public int find(T e) {
        for (int i=0; i < size; ++i) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    public void add(int index, T e) {
        checkIndexForAdd(index);
        if (size == data.length) {
            resize(2 * data.length);
        }

        for (int i=size-1;i>=index;i--) {
            data[i+1] = data[i];
        }

        data[index] = e;
        size++;
    }

    public void addFirst(T e) {
        add(0, e);
    }

    public T remove(int index) {
        checkIndex(index);
        T ret = data[index];
        for (int i = index+1;i<size;++i) {
            data[i-1]=data[i];
        }
        --size;
        data[size] = null;
        if (size == data.length / 4 && data.length / 2 !=0) {
            resize(data.length /2 );
        }

        return ret;
    }


    public T removeFirst() {
        return remove(0);
    }


    public T removeLast() {
        return remove(size -1);
    }

    public void removeElement(T e) {
        int index = find(e);
        if (index != -1) {
            remove(index);
        }
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(String.format("Array size=%d, capacity= %d \n", size, data.length));
        stringBuilder.append("[");
        for (int i=0;i < size; i++) {
            stringBuilder.append(data[i]);
            if (i != size -1) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    private void resize(int capacity) {
        T[] newArr = (T[]) new Object[capacity];
        for (int i=0;i<size;++i) {
            newArr[i] = data[i];
        }
        this.data = newArr;
    }

    private void checkIndex(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Add failed！Require index >= 0 and index < sie");
        }
    }

    private void checkIndexForAdd(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Remove failed！Require index >= 0 and size <= size");
        }
    }


    public static void main(String[] args) {
        GenericArray<Integer> genericArray = new GenericArray<Integer>(5);
//        genericArray.remove(0);
//        genericArray.remove(1);
        genericArray.add(0, 100);
        genericArray.add(1, 11);
        genericArray.add(2, 33);
        genericArray.add(3, 44);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        genericArray.add(4, 55);
        System.out.println(genericArray);
    }

}
